﻿using AjaxControlToolkit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class HR_AddEditEmployee : System.Web.UI.Page
{
    CommonMethods cMethod = new CommonMethods();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCompanies();
            BindBranches();
            BindDepartments();
            BindSubDepartment();
            if (Request.QueryString["EmpID"]!="" && Request.QueryString["EmpID"] != null)
            {
                GetEmployee(Convert.ToInt32(Request.QueryString["EmpID"].ToString()));
            }
        }
    }
    [WebMethod]
    public static string[] SearchCities(string prefixText, int count)
    {
        using (POSEntities db = new POSEntities())
        {

            var cities = (from c in db.Cities where c.City_Name.Contains(prefixText) select c).ToArray();
            return cities.Select(c => AutoCompleteExtender.CreateAutoCompleteItem(c.City_Name, c.City_ID.ToString())).ToArray();
        }
    }

    private void GetEmployee(int id)
    {
        using (POSEntities db = new POSEntities())
        {
            Int32 subdeptId = Convert.ToInt32(DDL_SubDepartment.SelectedValue);
            var employee = (from e in db.Employees
                             join d in db.Departments on e.Dept_ID equals d.Dept_ID
                             join sd in db.SubDepartments on e.SubDeptID equals sd.SubDeptID
                             join c in db.Cities on e.City_ID equals c.City_ID

                             where e.SubDeptID == subdeptId && e.IsDeleted == false
                             select new
                             {
                                 e.Branch.CompanyId,
                                 e.Branch.Company.CompanyName,
                                 e.BranchId,
                                 e.Branch.BranchName,
                                 d.Dept_ID,
                                 d.Dept_Name,
                                 sd.SubDeptID,
                                 sd.SubDeptName,
                                 e.EmpId,
                                 e.EmpName,
                                 e.Cell,
                                 e.Address,
                                 c.City_ID,
                                 c.City_Name
                             })
                          .FirstOrDefault();
            txtEmpName.Text = employee.EmpName;
            txtCellNo.Text = employee.Cell;
            txtAddress.Text = employee.Address;
            txtCity.Text = employee.City_Name;
            hdfCity.Value = employee.City_ID.ToString();
            DDL_Company.SelectedValue = employee.CompanyId.ToString();
            BindBranches();
            DDL_Branch.SelectedValue = employee.BranchId.ToString();
            BindDepartments();
            DDL_Department.SelectedValue = employee.Dept_ID.ToString();
            BindSubDepartment();
            DDL_SubDepartment.SelectedValue = employee.SubDeptID.ToString();
            btnSave.CommandArgument = employee.EmpId.ToString();
            btnSave.Text = "Update";
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        
        using (POSEntities db = new POSEntities())
        {
            try
            {
                if (btnSave.Text=="Save")
                {
                    Employee employee = new Employee();
                    employee.EmpName = txtEmpName.Text;
                    employee.City_ID = Convert.ToInt32(hdfCity.Value);
                    employee.Address = txtAddress.Text;
                    employee.Cell = txtCellNo.Text;
                    employee.BranchId = Convert.ToInt32(DDL_Branch.SelectedValue);
                    employee.Dept_ID = Convert.ToInt32(DDL_Department.SelectedValue);
                    employee.SubDeptID = Convert.ToInt32(DDL_SubDepartment.SelectedValue);
                    employee.EntryBy = 1;
                    employee.EntryDate = DateTime.Now;
                    db.Employees.Add(employee);
                    db.SaveChanges();
                    clearForm();
                }
                else if(btnSave.Text=="Update")
                {
                    int id=Convert.ToInt32(Request.QueryString["EmpID"].ToString());
                    var employee = (from emp in db.Employees where emp.EmpId == id select emp).FirstOrDefault();
                    employee.EmpName = txtEmpName.Text;
                    employee.City_ID = Convert.ToInt32(hdfCity.Value);
                    employee.Address = txtAddress.Text;
                    employee.Cell = txtCellNo.Text;
                    employee.BranchId = Convert.ToInt32(DDL_Branch.SelectedValue);
                    employee.Dept_ID = Convert.ToInt32(DDL_Department.SelectedValue);
                    employee.SubDeptID = Convert.ToInt32(DDL_SubDepartment.SelectedValue);
                    employee.EntryBy = 1;
                    employee.EntryDate = DateTime.Now;
                    db.SaveChanges();
                    GetEmployee(id);
                }
                
            }
            catch (Exception ex)
            {

            }

        }
    }
    private void BindDepartments()
    {
        if (DDL_Branch.Items.Count > 0)
        {
            DDL_Department.DataSource = cMethod.getDepartmentWithBranchId(Convert.ToInt32(DDL_Branch.SelectedValue));
            DDL_Department.DataBind();
        }
    }
    private void BindBranches()
    {
        if (DDL_Company.Items.Count > 0)
        {
            DDL_Branch.DataSource = cMethod.getBranchesWithCompanyId(Convert.ToInt32(DDL_Company.SelectedValue));
            DDL_Branch.DataBind();
        }
    }
    private void BindCompanies()
    {

        DDL_Company.DataSource = cMethod.getAllCompanies();
        DDL_Company.DataBind();

    }
    private void BindSubDepartment()
    {
        if (DDL_Department.Items.Count > 0)
        {
            DDL_SubDepartment.DataSource = cMethod.getSubDepartmentWithDepartmentId(Convert.ToInt32(DDL_Department.SelectedValue));
            DDL_SubDepartment.DataBind();
            if (DDL_SubDepartment.Items.Count==0)
            {
                btnSave.Enabled = false;
                btnSave.ToolTip = "No Department selected";
            }
            else
            {
                btnSave.Enabled = true;
                btnSave.ToolTip = "";
            }
        }
    }

    private void clearForm()
    {
        txtAddress.Text = "";
        txtCellNo.Text = "";
        txtCity.Text = "";
        txtEmpName.Text = "";
    }
    protected void DDL_Branch_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDepartments();
        BindSubDepartment();
    }

    protected void DDL_Department_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSubDepartment();
    }



    protected void DDL_Company_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindBranches();
        BindDepartments();
        BindSubDepartment();
    }
}